#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>

MQ::BindingManager::ptr bp;

class BindingTest : public testing::Environment
{
public:
    void SetUp() override
    {
        LOG(DEBUG, "测试环境初始化ing...\n");
        bp = std::make_shared<MQ::BindingManager>("./data/binding.db");
    }

    void TearDown() override
    {
        LOG(DEBUG, "测试环境清理ing...\n");
        // bp->clear();
    }
};

TEST(BindingTest, insert_test)
{
    bp->bind("exchange1", "queue1", "new.music.#", true);
    bp->bind("exchange1", "queue2", "new.pop.#", true);
    bp->bind("exchange1", "queue3", "new.music.#", true);
    bp->bind("exchange2", "queue1", "new.music.#", true);
    bp->bind("exchange2", "queue2", "new.pop.#", true);
    bp->bind("exchange2", "queue3", "new.music.#", true);
    bp->bind("exchange3", "queue1", "new.pop.#", true);
    bp->bind("exchange3", "queue2", "new.music.#", true);
    bp->bind("exchange3", "queue3", "new.pop.#", true);
    ASSERT_EQ(bp->size(), 9);
}

// TEST(BindingTest, delete_test)
// {
//     bp->unbind("exchange1", "queue1");
//     bp->unbind("exchange1", "queue3");
//     ASSERT_EQ(bp->size(), 7);
//     ASSERT_EQ(bp->exists("exchange1", "queue2"), true);
//     ASSERT_EQ(bp->exists("exchange2", "queue2"), true);
//     ASSERT_EQ(bp->exists("exchange3", "queue2"), true);
// }

// TEST(BindingTest, select_queue_test)
// {
//     bp->deleteQueuebinding("queue1");
//     ASSERT_EQ(bp->exists("exchange1", "queue1"), false);
//     ASSERT_EQ(bp->exists("exchange2", "queue2"), true);

//    auto it = bp->getBinding("exchange1","queue1");
//    ASSERT_EQ(it.get(), nullptr);
// }

// TEST(BindingTest, select_exchange_test)
// {
//     bp->deleteExchangebinding("exchange1");
//     ASSERT_EQ(bp->exists("exchange1", "queue1"), false);
//     ASSERT_EQ(bp->exists("exchange2", "queue2"), true);
// }

TEST(queue_test, select_test) 
{
    MQ::MsgQueueBindingMap mqbm = bp->getExchange("exchange2");
    ASSERT_NE(mqbm.find("queue1"), mqbm.end());
    ASSERT_NE(mqbm.find("queue2"), mqbm.end());
    ASSERT_NE(mqbm.find("queue3"), mqbm.end());
}


int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();
    return 0;
}